import roslib
roslib.load_manifest('rosbag')
import rosbag
import rospy
from rostopic import get_topic_class
from std_msgs.msg import String

# 参数初始化
input_bag = '/media/atakanoinu/MSDAD/[4]teachingarea1/[10]teachingarea1_dusk_cloudy（）/teachingarea1_dusk_cloudy.bag'
output_bag = '/media/atakanoinu/MSDAD/[4]teachingarea1/[10]teachingarea1_dusk_cloudy（）/teachingarea1_dusk_cloudy_rename.bag'
left_old_topic = '/camera/image_left'
left_new_topic = '/camera/image_right'
right_old_topic = '/camera/image_right'
right_new_topic = '/camera/image_left'
# 创建或打开输出的bag文件
outbag = rosbag.Bag(output_bag, 'w')
print("reading file")
# 读取输入的bag文件
for topic, msg, t in rosbag.Bag(input_bag).read_messages():
    # 如果是要更改的topic
    if topic == left_old_topic:
        # 更改topic名称
        outbag.write(left_new_topic, msg, t)
        print("change " + left_old_topic + " to " + left_new_topic)
    elif topic == right_old_topic:
        outbag.write(right_new_topic, msg, t)
        print("change " + right_old_topic + " to " + right_new_topic)
    else:
        # 其他topic不变
        outbag.write(topic, msg, t)
    
# 关闭bag文件
outbag.close()

# 参数初始化
input_bag = '/media/atakanoinu/MSDAD/[5]teachingarea2/[1]teachingarea2_midday（）/teachingarea2_midday.bag'
output_bag = '/media/atakanoinu/MSDAD/[5]teachingarea2/[1]teachingarea2_midday（）/teachingarea2_midday_rename.bag'
left_old_topic = '/camera/image_left'
left_new_topic = '/camera/image_right'
right_old_topic = '/camera/image_right'
right_new_topic = '/camera/image_left'
# 创建或打开输出的bag文件
outbag = rosbag.Bag(output_bag, 'w')
print("reading file")
# 读取输入的bag文件
for topic, msg, t in rosbag.Bag(input_bag).read_messages():
    # 如果是要更改的topic
    if topic == left_old_topic:
        # 更改topic名称
        outbag.write(left_new_topic, msg, t)
        print("change " + left_old_topic + " to " + left_new_topic)
    elif topic == right_old_topic:
        outbag.write(right_new_topic, msg, t)
        print("change " + right_old_topic + " to " + right_new_topic)
    else:
        # 其他topic不变
        outbag.write(topic, msg, t)
    
# 关闭bag文件
outbag.close()

# 参数初始化
input_bag = '/media/atakanoinu/MSDAD/[5]teachingarea2/[2]teachingarea2_dusk（）/teachingarea2_dusk.bag'
output_bag = '/media/atakanoinu/MSDAD/[5]teachingarea2/[2]teachingarea2_dusk（）/teachingarea2_dusk_rename.bag'
left_old_topic = '/camera/image_left'
left_new_topic = '/camera/image_right'
right_old_topic = '/camera/image_right'
right_new_topic = '/camera/image_left'
# 创建或打开输出的bag文件
outbag = rosbag.Bag(output_bag, 'w')
print("reading file")
# 读取输入的bag文件
for topic, msg, t in rosbag.Bag(input_bag).read_messages():
    # 如果是要更改的topic
    if topic == left_old_topic:
        # 更改topic名称
        outbag.write(left_new_topic, msg, t)
        print("change " + left_old_topic + " to " + left_new_topic)
    elif topic == right_old_topic:
        outbag.write(right_new_topic, msg, t)
        print("change " + right_old_topic + " to " + right_new_topic)
    else:
        # 其他topic不变
        outbag.write(topic, msg, t)
    
# 关闭bag文件
outbag.close()

# 参数初始化
input_bag = '/media/atakanoinu/MSDAD/[6]corridor1/[1]corridor1_midday（）/corridor1_midday.bag'
output_bag = '/media/atakanoinu/MSDAD/[6]corridor1/[1]corridor1_midday（）/corridor1_midday_rename.bag'
left_old_topic = '/camera/image_left'
left_new_topic = '/camera/image_right'
right_old_topic = '/camera/image_right'
right_new_topic = '/camera/image_left'
# 创建或打开输出的bag文件
outbag = rosbag.Bag(output_bag, 'w')
print("reading file")
# 读取输入的bag文件
for topic, msg, t in rosbag.Bag(input_bag).read_messages():
    # 如果是要更改的topic
    if topic == left_old_topic:
        # 更改topic名称
        outbag.write(left_new_topic, msg, t)
        print("change " + left_old_topic + " to " + left_new_topic)
    elif topic == right_old_topic:
        outbag.write(right_new_topic, msg, t)
        print("change " + right_old_topic + " to " + right_new_topic)
    else:
        # 其他topic不变
        outbag.write(topic, msg, t)
    
# 关闭bag文件
outbag.close()

# 参数初始化
input_bag = '/media/atakanoinu/MSDAD/[6]corridor1/[3]corridor1_dusk（）/corridor1_dusk.bag'
output_bag = '/media/atakanoinu/MSDAD/[6]corridor1/[3]corridor1_dusk（）/corridor1_dusk_rename.bag'
left_old_topic = '/camera/image_left'
left_new_topic = '/camera/image_right'
right_old_topic = '/camera/image_right'
right_new_topic = '/camera/image_left'
# 创建或打开输出的bag文件
outbag = rosbag.Bag(output_bag, 'w')
print("reading file")
# 读取输入的bag文件
for topic, msg, t in rosbag.Bag(input_bag).read_messages():
    # 如果是要更改的topic
    if topic == left_old_topic:
        # 更改topic名称
        outbag.write(left_new_topic, msg, t)
        print("change " + left_old_topic + " to " + left_new_topic)
    elif topic == right_old_topic:
        outbag.write(right_new_topic, msg, t)
        print("change " + right_old_topic + " to " + right_new_topic)
    else:
        # 其他topic不变
        outbag.write(topic, msg, t)
    
# 关闭bag文件
outbag.close()